package com.example.phoneshop.service.impl;

import com.example.phoneshop.dao.GoodsDao;
import com.example.phoneshop.entity.Goods;
import com.example.phoneshop.entity.PageBean;
import com.example.phoneshop.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * @author Chen
 * @date 2018/7/909:45
 */
@Service
public class GoodsServiceImpl implements GoodsService {
    private final GoodsDao goodsDao;
    private final ConcurrentMap<Long,Goods>concurrentMap=new ConcurrentHashMap<>();

    @Autowired
    public GoodsServiceImpl(GoodsDao goodsDao) {
        this.goodsDao = goodsDao;
    }

    @Override
    public Goods findGoodsById(int id) {
        return goodsDao.findGoodsById(id);
    }

    @Override
    public int addGoods(Goods goods) {
        return goodsDao.addGoods(goods);
    }

    @Override
    public int deleteGoodsById(int id) {
        return goodsDao.deleteGoodsById(id);
    }

    @Override
    public int updateGoods(Goods goods) {
        return goodsDao.updateGoods(goods);
    }

    @Override
    public List<Goods> findGoods(Map<String, Object> map) {
        return goodsDao.findGoods(map);
    }

    @Override
    public int insertGoodType(String name) {
        return goodsDao.insertGoodType(name);
    }

    @Override
    public Long getTotalGoods(Map<String, Object> map) {
        return goodsDao.getTotalGoods(map);
    }

    @Override
    public List<Goods> productList(){return new ArrayList<>(this.concurrentMap.values()); }

    @Override
    public int addGoodsByMap(Map map){ return goodsDao.addGoodsByMap(map);}

    @Override
    public int addGoodsImg(Map map){return goodsDao.addGoodsImg(map);}

    @Override
    public PageBean<Goods> findGoodsByPage(int page){
        HashMap<String,Object> map=new HashMap<>();
        PageBean<Goods> pageBean=new PageBean<>(page,5);
        int totalCount=goodsDao.goodsCount();
        Double num=Math.ceil((double) totalCount /5);
        pageBean.setTotalPage(num.intValue());
        map.put("start",(page-1)*5);
        map.put("size",pageBean.getPageSize());
        List<Goods> list=goodsDao.findGoods(map);
        pageBean.setList(list);
        return pageBean;
    }
}
